package org.aaron.demos.myviews;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

public class CanvasView extends View {

	public CanvasView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawColor(Color.WHITE);
		// 设置画笔属性
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(Color.BLUE);
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(3);

		// 设置画笔填充风格
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.RED);

		// 设置渐变器
		Shader shader = new LinearGradient(0, 0, 40, 60, new int[] { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW }, null, Shader.TileMode.REPEAT);
		paint.setShader(shader);

		// 画圆形
		canvas.drawCircle(40, 40, 30, paint);

		// 画正方形
		canvas.drawRect(10, 80, 70, 140, paint);

		// 画矩形
		canvas.drawRect(10, 150, 70, 190, paint);

		// 圆角矩形
		RectF rect = new RectF(10, 200, 70, 240);
		canvas.drawRoundRect(rect, 15, 15, paint);

		// 椭圆
		RectF rectF = new RectF(10, 240, 70, 270);
		canvas.drawOval(rectF, paint);

		// 三角形
		Path path3 = new Path();
		path3.moveTo(10, 340);
		path3.lineTo(70, 340);
		path3.lineTo(40, 290);
		canvas.drawPath(path3, paint);
		path3.close();

		Path path5 = new Path();
		path5.moveTo(26, 360);
		path5.lineTo(54, 360);
		path5.lineTo(70, 392);
		path5.lineTo(40, 420);
		path5.lineTo(10, 392);
		path5.close();

	}
}
